package dao

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
	"github.com/spf13/viper"
	"pqstat/pkg/log"
	"time"
)

func Connect() {
	viper.AutomaticEnv()
	host := viper.GetString("PQ_MYSQL_HOST")
	user := viper.GetString("PQ_MYSQL_USERNAME")
	pwd := viper.GetString("PQ_MYSQL_PASSWORD")
	port := viper.GetString("PQ_MYSQL_PORT")
	db := viper.GetString("PQ_MYSQL_DB")
	log.Debug(host)
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&&loc=Local", user, pwd, host, port, db)
	log.Debug(dsn)
	err := orm.RegisterDataBase("default", "mysql", dsn)
	if err != nil {
		log.Fatal("Mysql connection fail:" + err.Error())
	} else {
		//设置最大链接数
		orm.SetMaxIdleConns("default", 100)
		orm.SetMaxOpenConns("default", 300)
		orm.RegisterModel(&PqUserStat{})
		orm.Debug = true
	}
}

type BaseDao struct {
	CreatedTime time.Time `orm:"auto_now_add;type(datetime)" json:"created_time"`
	UpdatedTime time.Time `orm:"auto_now;type(datetime)" json:"updated_time"`
}
